package com.cjc.sport.controller;

import com.cjc.sport.entity.EnumValidation;
import com.cjc.sport.entity.NumEnum;
import com.cjc.sport.entity.ReturnJson;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * ========================
 * Created with intellij IDEA
 * 创建者：杜一平
 * 日期：2017/1/22（星期一）
 * 时间：08：00
 * 说明：错误页面提示内容
 * ==========================
 */
@Api(description = "错误模块")
@RestController
public class ErrorController implements HandlerInterceptor {

    /**
     * 日志配置
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(ErrorController.class);

    /**
     * 拦截400错误
     *
     * @return ReturnJson 封装json对象
     */
    @RequestMapping("/400")
    public ReturnJson badRequest() {
        LOGGER.error("It's a 400 error");
        return new ReturnJson(NumEnum.FAIL.getIndex(), "您输入的字段有误，请重新输入!");
    }

    /**
     * 拦截404错误
     *
     * @return ReturnJson 封装json对象
     */
    @RequestMapping("/404")
    public ReturnJson notFound() {
        LOGGER.error("It's a 404 error");
        return new ReturnJson(NumEnum.FAIL.getIndex(), "404错误!");
    }

    /**
     * 拦截500错误
     *
     * @return ReturnJson 封装json对象
     */
    @RequestMapping("/500")
    public ReturnJson serverEorror() {
        LOGGER.error("It's a 500 error");
        return new ReturnJson(NumEnum.FAIL.getIndex(), "500错误!");
    }

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        try {
            if (httpServletResponse.getStatus() == EnumValidation.FOURHZERO.getNum()) {
                modelAndView.setViewName("/errorpage/400");
            }
            if (httpServletResponse.getStatus() == EnumValidation.FOURHFOUR.getNum()) {
                modelAndView.setViewName("errorpage/404");
//            modelAndView.addObject( new ReturnJson(NumEnum.FAIL.getIndex(), "404错误!", null, 0));
            }
            if (httpServletResponse.getStatus() == EnumValidation.FIVEHZREO.getNum()) {
                modelAndView.setViewName("/errorpage/500");
            }
        } catch (Exception e) {
            LOGGER.error("It is a normal Exception when this project run in IDEA!");
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }


}
